<?php
class M_ACL_User extends MVC_Model_Base implements ACL_User_Interface {

    private $_arData = array(
        'login' => '@guest',
        'mail' => null,
        'password' => null,
        'privilege' => null
    );


    private $_objAccess = null;


    public function athorization (ACL_AthorizationData_Interface $objData) {
        if ($this->getLogin() != '@guest') {
            return false;
        }
        
        $arAccount = $this->_getAccount($objData);
        if (!empty($arAccount)) {
            $this->_arData = $arAccount;
            return true;
        }
        
        return false;
    }


    public function getLogin () {
        return $this->_arData['login'];
    }


    public function getMail () {
        return $this->_arData['mail'];
    }


    public function getPrivilege () {
        return $this->_arData['privilege'];
    }


    public function getAccess() {
        if (empty($this->_objAccess)) {
            $this->_objAccess = $this->getModel('ACL_Access');
        }
        return $this->_objAccess;
    }


    private function _getAccount (ACL_AthorizationData_Interface $objData) {
        $objUsers = $this->getDbReader('MAIN', 'Acp_Users');
        return $objUsers->elementBy(
            'login = ? AND password = ?',
            $objData->getLogin(),
            $objData->getPassword()
        );
    }


    public function isCorrectAuthorizationData($strLogin, $strPassword) {
        $objUsers = $this->getDbReader('MAIN', 'Acp_Users');
        return $objUsers->isExistsByWhere('login = ? AND password = MD5(?)', $strLogin, $strPassword);
    }
}